<?php

/**
 * ==============================================
 * Created by Finger Technology.
 * Author: ZhiHuaWei <zhihua_wei@foxmail.com>
 * Date: 2019/8/26 17:33
 * Project: YunAdmin
 * Power: YunAdmin 文章管理控制器
 * Version: 1.0.0
 * ==============================================
 */

namespace app\admin\controller;

use think\Db;

/**
 * 文章类
 * Class Article
 * @package app\admin\controller
 */
class Article extends YunAdmin
{
    /**
     * 文章列表
     * @return mixed
     */
    public function index()
    {
        $art_list = Db::name('article')->field('article_id,category_id,article_title,keywords,article_desc,create_time,modify_time')->where(['is_delete' => 0])->order("modify_time DESC")->paginate(10);
        $this->assign('list', $art_list);
        return $this->fetch();
    }

    /**
     * 新增文章
     * @return mixed
     */
    public function add()
    {
        $cate_list = Db::name('category')->field('category_id,category_name')->where(['is_delete' => 0])->order("sort ASC")->select();
        if (empty($cate_list)) {
            $this->error('当前无文章类别信息，请调整后文章分类后再进行添加文章！', url('index'));
        }
        $this->assign('cate_list', $cate_list);
        return $this->fetch();
    }

    /**
     * 修改文章
     * @return mixed
     */
    public function edit()
    {
        $id = input('id', 0);

        if ($id) {
            $article_info = Db::name('article')->find($id);
            $cate_list = Db::name('category')->field('category_id,category_name')->where(['is_delete' => 0])->order("sort ASC")->select();
            $this->assign('cate_list', $cate_list);
            $this->assign('article', $article_info);
            return $this->fetch();
        } else {
            $this->error('系统异常，请重试！');
        }
    }

    /**
     * 保存文章
     */
    public function save()
    {
        # 1 获得文章信息
        $article_info = [
            'category_id' => input('post.category_id', 0),
            'article_title' => input('post.article_title', ''),
            'keywords' => input('post.keywords', ''),
            'article_desc' => input('post.article_desc', ''),
            'content' => input('post.content', ''),
            'modify_time' => date("Y-m-d H:i:s"),
        ];

        # 2 处理上传图片
        $upload_img = new Uploads();
        $article_img = $upload_img->uploadArticleImg('article_pic', 'article_picture');
        if ($article_img['code']) {
            $article_info['article_pic'] = $article_img['data']['img'];
            $article_info['thumb_pic'] = $article_img['data']['thumb_img'];
        }

        # 3 根据是否有id判断是新增还是修改
        $id = input('post.id', 0);
        if ($id) {
            # 4 修改文章信息
            $ret = Db::name('article')->where(['article_id' => $id])->update($article_info);
            if ($ret !== false) {
                $this->success('恭喜您，文章信息修改成功！', url('index'));
            } else {
                $this->error('文章信息修改失败，请重试！');
            }
        } else {
            # 5 新增文章分类信息
            $article_info['create_time'] = $article_info['modify_time'];
            $ret = Db::name('article')->insert($article_info);
            if ($ret !== false) {
                $this->success('恭喜您，文章信息新增成功！', url('index'));
            } else {
                $this->error('文章信息新增失败，请重试！');
            }
        }
    }

    /**
     * 删除文章
     */
    public function del()
    {
        # 1 获取需删除的文章id
        $id = input('id', 0);
        if ($id) {
            # 2 进行软删除操作
            $article_del = [
                'is_delete' => 1,
                'modify_time' => date("Y-m-d H:i:s"),
            ];
            $ret = Db::name('article')->where(['article_id' => $id])->update($article_del);
            # 3 结果返回
            if ($ret !== false) {
                $this->success('恭喜您，文章信息删除成功！', url('index'));
            } else {
                $this->error('文章信息删除失败，请重试！');
            }
        } else {
            $this->error('系统异常，请重试！');
        }
    }
}